using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using System.IO;

namespace Brutal_Dare_Portable
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public enum GameState { SplashScreen, StartScreen, InstructionScreen, Passing, EndPassing, Choose, Truth, Dare, TruthPenalty, DarePenalty, Pause, EndPause, EndGame }

        public Texture2D logo;
        public string logoText = "Brutal Dare";
        public string[][] questions;
        public SpriteFont introFont, titleFont, subTextFont, logoFont, choiceFont;

        public InputManager inputManager;
        public SplashScreen splashScreen;
        public StartScreen startScreen;
        public InstructionScreen instructionScreen;
        public Passing passing;
        public EndPassing endPassing;
        public Choose choose;
        public Truth truth;
        public TruthPenalty truthPenalty;
        public Dare dare;
        public DarePenalty darePenalty;
        public Pause pause;
        public EndPause endPause;
        public EndGame endGame;

        public GameState currentGameState = GameState.Choose, previousGameState, previousPauseState;

        public bool startTransition = false, finishTransition = false, under18 = true;

        public Random rand;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.IsFullScreen = true;

            rand = new Random();

            questions = new string[4][];
            questions[0] = new string[6];
            questions[1] = new string[20];
            questions[2] = new string[10];
            questions[3] = new string[23];
            

            LoadTextFiles();

            inputManager = new InputManager(this);
            inputManager.Enabled = true;
            Components.Add(inputManager);

            splashScreen = new SplashScreen(this);
            splashScreen.Enabled = false;
            splashScreen.Visible = false;
            Components.Add(splashScreen);

            startScreen = new StartScreen(this);
            startScreen.Enabled = false;
            startScreen.Visible = false;
            Components.Add(startScreen);

            instructionScreen = new InstructionScreen(this);
            instructionScreen.Enabled = false;
            instructionScreen.Visible = false;
            Components.Add(instructionScreen);

            passing = new Passing(this);
            passing.Enabled = false;
            passing.Visible = false;
            Components.Add(passing);

            endPassing = new EndPassing(this);
            endPassing.Enabled = false;
            endPassing.Visible = false;
            Components.Add(endPassing);

            choose = new Choose(this);
            choose.Enabled = false;
            choose.Visible = false;
            Components.Add(choose);

            truth = new Truth(this);
            truth.Enabled = false;
            truth.Visible = false;
            Components.Add(truth);

            truthPenalty = new TruthPenalty(this);
            truthPenalty.Enabled = false;
            truthPenalty.Visible = false;
            Components.Add(truthPenalty);

            dare = new Dare(this);
            dare.Enabled = true;
            dare.Visible = true;
            Components.Add(dare);

            darePenalty = new DarePenalty(this);
            darePenalty.Enabled = false;
            darePenalty.Visible = false;
            Components.Add(darePenalty);

            pause = new Pause(this);
            pause.Enabled = false;
            pause.Visible = false;
            Components.Add(pause);

            endPause = new EndPause(this);
            endPause.Enabled = false;
            endPause.Visible = false;
            Components.Add(endPause);

            endGame = new EndGame(this);
            endGame.Enabled = false;
            endGame.Visible = false;
            Components.Add(endGame);

            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            introFont = Content.Load<SpriteFont>(@"fonts/splashscreenfont");
            titleFont = Content.Load<SpriteFont>(@"fonts/titlefont");
            subTextFont = Content.Load<SpriteFont>(@"fonts/subtextfont");
            logoFont = Content.Load<SpriteFont>(@"fonts/logoFont");
            choiceFont = Content.Load<SpriteFont>(@"fonts/choiceFont");

            logo = Content.Load<Texture2D>(@"textures/dudeandgreb");

            base.LoadContent();
        }


        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {

            base.Draw(gameTime);
        }

        private void LoadTextFiles()
        {
            StreamReader sr;
            for (int i = 0; i < 4; i++ )
            {
                if (i == 0)
                {
                    sr = new StreamReader(TitleContainer.OpenStream(@"Content\Text Files\dares.txt"));
                    for (int j = 0; j<6; j++)
                    {
                        questions[i][j] = sr.ReadLine();
                    }
                }
                else if (i == 1)
                {
                    sr = new StreamReader(TitleContainer.OpenStream(@"Content\Text Files\dares18.txt"));
                    for (int j = 0; j < 20; j++)
                    {
                        questions[i][j] = sr.ReadLine();
                    }
                }
                else if (i == 2)
                {
                    sr = new StreamReader(TitleContainer.OpenStream(@"Content\Text Files\truths.txt"));
                    for (int j = 0; j < 10; j++)
                    {
                        questions[i][j] = sr.ReadLine();
                    }
                }
                else
                {
                    sr = new StreamReader(TitleContainer.OpenStream(@"Content\Text Files\truths18.txt"));
                    for (int j = 0; j < 10; j++)
                    {
                        questions[i][j] = sr.ReadLine();
                    }
                }
            }
        }

        public void Transition()
        {

        }

        public void ChangeAgeGroup()
        {
            if (under18 == true)
            {
                under18 = false;
            }
            else
            {
                under18 = true;
            }
        }

        #region Gamestate handling ...
        public void ChangeGameState(GameState gameState)
        {
            previousGameState = currentGameState;
            currentGameState = gameState;

            switch (gameState)
            {
                case GameState.StartScreen:
                    splashScreen.Visible = false;
                    splashScreen.Enabled = false;

                    startScreen.Visible = true;
                    startScreen.Enabled = true;
                    break;

                case GameState.InstructionScreen:
                    startScreen.Visible = false;
                    startScreen.Enabled = false;

                    instructionScreen.Visible = true;
                    instructionScreen.Enabled = true;
                    break;

                case GameState.Passing:
                    switch (previousGameState)
                    {
                        case GameState.InstructionScreen:
                            instructionScreen.Visible = false;
                            instructionScreen.Enabled = false;
                            break;
                        case GameState.Truth:
                            truth.Visible = false;
                            truth.Enabled = false;
                            break;
                        case GameState.TruthPenalty:
                            truthPenalty.Visible = false;
                            truthPenalty.Enabled = false;
                            break;
                        case GameState.Dare:
                            dare.Visible = false;
                            dare.Enabled = false;
                            break;
                        case GameState.DarePenalty:
                            darePenalty.Visible = false;
                            darePenalty.Enabled = false;
                            break;
                    }
                    passing.Visible = true;
                    passing.Enabled = true;
                    break;

                case GameState.EndPassing:
                    passing.Visible = false;
                    passing.Enabled = false;

                    endPassing.Visible = true;
                    endPassing.Enabled = true;
                    break;

                case GameState.Choose:
                    endPassing.Visible = false;
                    endPassing.Enabled = false;

                    choose.Visible = true;
                    choose.Enabled = true;
                    break;

                case GameState.Truth:
                    choose.Visible = false;
                    choose.Enabled = false;

                    truth.Visible = true;
                    truth.Enabled = true;
                    break;

                case GameState.TruthPenalty:
                    truth.Visible = false;
                    truth.Enabled = false;

                    truthPenalty.Visible = true;
                    truthPenalty.Enabled = true;
                    break;

                case GameState.Dare:
                    choose.Visible = false;
                    choose.Enabled = false;

                    dare.Visible = true;
                    dare.Enabled = true;
                    break;

                case GameState.DarePenalty:
                    dare.Visible = false;
                    dare.Enabled = false;

                    darePenalty.Visible = true;
                    darePenalty.Enabled = true;
                    break;

                case GameState.Pause:
                    switch (previousGameState)
                    {
                        case GameState.Choose:
                            choose.Enabled = false;
                            break;
                        case GameState.Passing:
                            passing.Enabled = false;
                            break;
                        case GameState.EndPassing:
                            endPassing.Enabled = false;
                            break;
                        case GameState.Truth:
                            truth.Enabled = false;
                            break;
                        case GameState.TruthPenalty:
                            truthPenalty.Enabled = false;
                            break;
                        case GameState.Dare:
                            dare.Enabled = false;
                            break;
                        case GameState.DarePenalty:
                            darePenalty.Enabled = false;
                            break;
                    }

                    previousPauseState = previousGameState;
                    pause.Visible = true;
                    pause.Enabled = true;
                    break;

                case GameState.EndPause:
                    switch (previousPauseState)
                    {
                        case GameState.Choose:
                            choose.Enabled = true;
                            break;
                        case GameState.Passing:
                            passing.Enabled = true;
                            break;
                        case GameState.EndPassing:
                            endPassing.Enabled = true;
                            break;
                        case GameState.Truth:
                            truth.Enabled = true;
                            break;
                        case GameState.TruthPenalty:
                            truthPenalty.Enabled = true;
                            break;
                        case GameState.Dare:
                            dare.Enabled = false;
                            break;
                        case GameState.DarePenalty:
                            darePenalty.Enabled = false;
                            break;
                    }
                    pause.Visible = false;
                    pause.Enabled = false;
                    break;

                case GameState.EndGame:
                    switch (previousPauseState)
                    {
                        case GameState.InstructionScreen:
                            break;
                        case GameState.Passing:
                            break;
                        case GameState.EndPassing:
                            break;
                        case GameState.Truth:
                            break;
                        case GameState.TruthPenalty:
                            break;
                        case GameState.Dare:
                            break;
                        case GameState.DarePenalty:
                            break;
                    }
                    endGame.Visible = true;
                    endGame.Enabled = true;
                    break;
            }
        #endregion
        }
    }
}
